// 注册表
// require(["../require.config"],function(){
//     console.log("99");
// })
;; (function ($) {
    //性别判断
    var xyyc1 = document.querySelector(".xyyc1");
    var xyyc2 = document.querySelector(".xyyc2");
    var xxy = document.querySelectorAll(".xxy");
    xxy[0].addEventListener("click", dispaly);

    function dispaly() {
        xyyc2.style.background = "#fff";
        xyyc1.style.background = "#000";

    }
    xxy[1].addEventListener("click", dispaly1);
    function dispaly1() {
        xyyc1.style.background = "#fff";
        xyyc2.style.background = "#000";
    }

    // 登录的帐号密码 验证

    var alogin = $(".inputnumber")[0];
    var span = document.querySelector(".span_content");
    var inputborder = document.querySelectorAll(".r_l_c_group1_a")[0];
    var TEL_REGEXP = /^1([38][0-9]|4[579]|5[0-3,5-9]|6[6]|7[0135678]|9[89])\d{8}$/;
    var re = /^[A-Za-z\d]+([-_.][A-Za-z\d]+)*@([A-Za-z\d]+[-.])+[A-Za-z\d]{2,4}$/;
    // 密码
    var patrn = /^(\w){6,20}$/;

    function verification() {
        var loginvalue = alogin.value;
        if (loginvalue === "") {
            span.innerHTML = "登录名不能为空";
            alogin.focus();
            span.style.display = "block";
            inputborder.style.borderColor = "red";
            return false;
        }
        if (typeof Number(loginvalue) === "number" && !TEL_REGEXP.test(loginvalue)) {
            span.innerHTML = "请输入正确的手机号";
            span.style.display = "block";
            alogin.focus();
            inputborder.style.borderColor = "red";
            return false;
        }
        if (!re.test(loginvalue) && loginvalue.length != 11) {
            span.innerHTML = "请输入正确的邮箱";
            span.style.display = "block";
            alogin.focus();
            inputborder[0].style.borderColor = "red";
            return false;
        } else {
            span.style.display = "none";
            inputborder.style.borderColor = "#dadada";
            return true;
        }
    }
    alogin.addEventListener("blur", verification);

    // 密码强度
    var password = $(".inputnumber")[2];
    var inputborder1 = document.querySelectorAll(".r_l_c_group1_a")[2];
    var inputborder2 = document.querySelectorAll(".r_l_c_group1_a")[3];
    var againpassword = $(".inputnumber")[3];
    var span1 = $(".span1");
    var span2 = $(".span2");
    var r1 = $("#r_l_c_group1");
    // 这个监听事件怎么都不能用on???????
    password.addEventListener("blur", pass);
    againpassword.addEventListener("blur", pass2)
    function pass() {
        var strength = 0;
        var str = password.value;
        if (str.length === 0) {
            span1.text("请输入您的密码");
            span1.addClass("span2");
            password.focus();
            inputborder1.style.borderColor = "red";
        }
        for (var i = 0; i < str.length; i++) {
            if (str[i] >= 0 && str[i] <= 9) {
                //这里面存在数字;
                strength++;
                break;
            }

        }
        for (var i = 0; i < str.length; i++) {
            var code = str[i].charCodeAt(0);
            if (code >= 65 && code <= 90 || code >= 97 && code <= 122) {
                //这里面存在字母;
                strength++;
                break;
            }
        }
        // 特殊符号
        for (var i = 0; i < str.length; i++) {
            var code = str[i].charCodeAt(0);
            var flag = false;
            switch (code) {
                case 33:
                case 65:
                case 35:
                case 36:
                    strength++;
                    flag = !flag;
                    break;
            }
            if (flag) {
                break;
            }
        }
        color(strength);
    }
    function color(strength) {
        switch (strength) {
            case 1: r1.addClass('r_red'); span1.text("非常弱"); break;
            case 2: r1.addClass('r_yellow'); span1.text("普通"); break;
            case 3: r1.addClass('r_green'); span1.text("安全"); break;

        }
    }
    // 再次输入密码
    function pass2() {
        var againvalue = againpassword.value;
        console.log(againvalue, password.value);
        if (againvalue != password.value) {
            span2.text("两次输入密码不一致");
            againpassword.focus();
            inputborder2.style.borderColor = "red";
            return false;
        } else {
            inputborder2.style.borderColor = "#000";
            span2.text("密码一致");
        }
    }
    // 随机图片的生成
    /**生成一个随机数**/
    function randomNum(min, max) {
        return Math.floor(Math.random() * (max - min) + min);
    }
    /**生成一个随机色**/
    function randomColor(min, max) {
        var r = randomNum(min, max);
        var g = randomNum(min, max);
        var b = randomNum(min, max);
        return "rgb(" + r + "," + g + "," + b + ")";
    }
    drawPic();
    document.getElementById("changeImg").onclick = function (e) {
        e.preventDefault();
        drawPic();
    }

    /**绘制验证码图片**/
    function drawPic() {
        var canvas = document.getElementById("canvas");
        var width = canvas.width;
        var height = canvas.height;
        var ctx = canvas.getContext('2d');
        ctx.textBaseline = 'bottom';

        /**绘制背景色**/
        ctx.fillStyle = randomColor(180, 240); //颜色若太深可能导致看不清
        ctx.fillRect(0, 0, width, height);
        /**绘制文字**/
        var str = 'ABCEFGHJKLMNPQRSTWXY123456789';
        for (var i = 0; i < 4; i++) {
            var txt = str[randomNum(0, str.length)];
            ctx.fillStyle = randomColor(50, 200); //随机生成字体颜色
            ctx.font = randomNum(40, 80) + 'px SimHei'; //随机生成字体大小
            var x = 10 + i * 50;
            var y = randomNum(50, 150);
            var deg = randomNum(-45, 45);
            //修改坐标原点和旋转角度
            ctx.translate(x, y);
            ctx.rotate(deg * Math.PI / 180);
            ctx.fillText(txt, 0, 0);
            //恢复坐标原点和旋转角度
            ctx.rotate(-deg * Math.PI / 180);
            ctx.translate(-x, -y);
        }
        /**绘制干扰线**/
        // for(var i = 0; i < 8; i++) {
        // ctx.strokeStyle = randomColor(40, 180);
        // ctx.beginPath();
        // ctx.moveTo(randomNum(0, width), randomNum(0, height));
        // ctx.lineTo(randomNum(0, width), randomNum(0, height));
        // ctx.stroke();
        // }
        /**绘制干扰点**/
        // for(var i = 0; i < 100; i++) {
        // ctx.fillStyle = randomColor(0, 255);
        // ctx.beginPath();
        // ctx.arc(randomNum(0, width), randomNum(0, height), 1, 0, 2 * Math.PI);
        // ctx.fill();
        // }
    }
    // 设置cookie
    function xhrPost(url, data) {
        return new Promise((resolve, reject) => {
            var xhr = new XMLHttpRequest();
            xhr.open("POST", url);
            xhr.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
            var _data = [];
            for (let key in data) {
                _data.push(`${key}=${data[key]}`);
            }
            xhr.send(_data.join("&"));
            xhr.onload = function () {
                xhr.status === 200 ? resolve(xhr.responseText) : reject(xhr.status);
            }
        })
    }
    //cookie的设置
    function cookie(key,value,options){
        typeof options === "object" ? Object.assign({},options) : options = {}; 
        var res = "";
        res += key + "=" + encodeURI(value);
        if(typeof options.expires === "number"){
              var d = new Date()
              
              d.setDate(d.getDate() + options.expires);
              res += ";expires="+d;
        }
        res += options.path ? ";path="+options.path : "";
        res += options.domain ? ";domain="+options.domain : ""
        document.cookie = res;
  }
    var username = document.getElementById("username");
    var passwordbtn = document.getElementById("password");
    var registerbtn = document.getElementById("registerbtn");
    registerbtn.onclick = function () {
        var uservalue = username.value;
        var psdvalue = passwordbtn.value;
        //console.log(uservalue, psdvalue);
        xhrPost("http://zhaoyx0907.com/users/register.do", { username: uservalue, password: psdvalue })
            .then(res => {
                //console.log(res);
                if (res === "0") {
                    //state.innerHTML = "已登录";
                    console.log(cookie)
                    cookie("username", uservalue, {
                        expires: 7,
                        path : "/"
                    });
                    alert("注册成功");
                    text1(uservalue);
                    //location.href = "./index.html";
                }
            })
    }
    var text = document.getElementById("loginreg");
    function text1(value){
        if(value!=""){
           text.innerHTML=value; 
        }
        
    }

})(jQuery);